#pragma once

#include <windows.h>

namespace PathUtils
{
	class W
	{
	private:
		char * we;
		size_t w_size;
		wchar_t * e;
		size_t e_size;
	public:

		W(const wchar_t* x)
		{
			size_t y = wcslen(x);

			w_size = WideCharToMultiByte(
				CP_UTF8,0,x,-1,
				we,0,
				0,0);

			we = new char[w_size];
			memset(we,0,w_size);

			w_size = WideCharToMultiByte(
				CP_UTF8,0,x,-1,
				we,(int)w_size,
				0,0);

			e = new wchar_t[y];
			memmove(e,x,y * sizeof(wchar_t));
			e_size = y;
		}

		W(const char * x)
		{
			size_t y = strlen(x);
			w_size = y;

			we = new char[y];
			memmove(we,x,y * sizeof(char));

			e_size = MultiByteToWideChar(CP_UTF8,
				0,
				x,
				-1,
				e,
				0);

			e = new wchar_t[e_size];
			memset(e,0,e_size * sizeof(wchar_t));

			e_size = MultiByteToWideChar(CP_UTF8,
				0,
				x,
				-1,
				e,
				(int)e_size);
		}

		~W()
		{
			if (we != NULL)
				delete[] we;

			if (e != NULL)
				delete[] e;
		}

		operator char* ()
		{
			return we;
		}

		operator wchar_t* ()
		{
			return e;
		}

		char * DetachChar() { char * ret = we; we = NULL; return ret; }
		wchar_t * DetachWChar() { wchar_t * ret = e; e = NULL; return ret; }

		size_t GetCharLen() const { return w_size; }
		size_t GetWCharLen() const { return e_size; }
	};
}